package org.abettor.angleworm;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.Method;

import org.abettor.util.ClassPathLoader;
import org.abettor.util.Environment;

/**
 * 类加载器，自动加载所需classpath，并执行入口程序
 * @author abettor
 *
 */
public class BootEntry
{
	public static void main(String[] args) throws Exception
	{
		//	加载工程中的classpath
		{
			String libPath = Environment.getFilePath("lib");
			File libFolder = new File(libPath);
			File[] libs = libFolder.listFiles(new FilenameFilter()
			{
				@Override
				public boolean accept(File file, String fileName)
				{
					int len = fileName.length();
					if(len > 4)
					{
						String ext = fileName.substring(len - 4);
						if(ext.equalsIgnoreCase(".jar"))
						{
							return true;
						}
					}

					return false;
				}
			});
			for(File file : libs)
			{
				ClassPathLoader.addFile(file);
			}
		}

		//	加载OS相关的classpath
		{
			String nativeLibPath = Environment.getFilePath("lib/jni");
			File nativeLib = new File(nativeLibPath);
			File[] libs = nativeLib.listFiles(new FilenameFilter()
			{
				@Override
				public boolean accept(File file, String fileName)
				{
					int len = fileName.length();
					if(len > 4)
					{
						String ext = fileName.substring(len - 4);
						if(ext.equalsIgnoreCase(".jar"))
						{
							return true;
						}
					}

					return false;
				}
			});
			for(File file : libs)
			{
				ClassPathLoader.addFile(file);
			}
		}

		//	加载入口程序
		Class<?> clazz = Class.forName("org.abettor.angleworm.Angleworm");
		Class<?>[] paramTypes = new Class<?>[]{String[].class};
		Method method = clazz.getMethod("main", paramTypes);
		Object[] objs = new Object[]{new String[]{}};
		method.invoke(null, objs);
	}
}
